import { Models } from '../common/typings/modle'

import { ParameterException } from '../core/HttpException'
import { validate } from '../server/ajv'
type RequestDataType = 'query' | 'body'

function validator(schema: string | boolean | object, type: RequestDataType = 'query') {
  return async function validator(ctx: Models.Ctx, next: Function) {
    const data = ctx.request[type] as object
    const errors = validate(schema, data) || null
    if(errors){
      // 
      throw new ParameterException(errors)
    }
    await next()
  }
}


export default validator